package utfpr.gang5.hermes.controledepresenca.util;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.classic.Session;

import utfpr.gang5.hermes.controledepresenca.Factory;
import utfpr.gang5.hermes.controledepresenca.dao.CrudException;
import utfpr.gang5.hermes.controledepresenca.pojo.Aluno;
import utfpr.gang5.hermes.controledepresenca.pojo.CalendarioSemanal;
import utfpr.gang5.hermes.controledepresenca.pojo.Curso;
import utfpr.gang5.hermes.controledepresenca.pojo.Disciplina;
import utfpr.gang5.hermes.controledepresenca.pojo.Professor;
import utfpr.gang5.hermes.controledepresenca.services.AlunoService;
import utfpr.gang5.hermes.controledepresenca.services.CalendarioSemanalService;
import utfpr.gang5.hermes.controledepresenca.services.CursoService;
import utfpr.gang5.hermes.controledepresenca.services.DisciplinaService;
import utfpr.gang5.hermes.controledepresenca.services.ProfessorService;


public class GeraBanco 
{
	
	public static String[] NOMES_PROFESSORES = new String[] {
		"Hermes Dalmont", "Claudio Bazzi", "Fernando Shutz", "Alessandra Hoffman", "Everton Coimbra"
	};
	
	public static String[] NOMES_DISCIPLINAS = new String[] {
		"Algoritimos", "LP 1", "LP 2", "Calculo", "Estatística"
	};
	
		public static void main(String[] args)
		{
			HibernateUtil.gerarBanco(false);
			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			try {
				List<Professor> professores = criarProfessores();
				Curso curso = inserirCurso();
				List<Disciplina> disciplinas = inserirDisciplinas(professores, curso);
				inserirAluno(curso);
				inserirAluno2(curso);
				
				
				CalendarioSemanalService daoCal = Factory.getCalendarioSemanalService();
				inserirCalendarioSegunda(disciplinas, daoCal);
				inserirCalendarioTerca(disciplinas, daoCal);
				inserirCalendarioQuarta(disciplinas, daoCal);
				inserirCalendarioQuinta(disciplinas, daoCal);
				inserirCalendarioSexta(disciplinas, daoCal);
				
				
				session.getTransaction().commit();
			} catch (Exception e) {
				e.printStackTrace();
				session.getTransaction().rollback();
			} finally {
				if (session.isOpen())
					session.close();
			}
			
		}

		private static void inserirCalendarioSegunda(
				List<Disciplina> disciplinas, CalendarioSemanalService daoCal)
				throws CrudException {
			CalendarioSemanal c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.SEG);
			c.setAula(1);
			c.setDisciplina(disciplinas.get(0));
			daoCal.salvar(c);
			
			c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.SEG);
			c.setAula(2);
			c.setDisciplina(disciplinas.get(0));
			daoCal.salvar(c);
			
			c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.SEG);
			c.setAula(3);
			c.setDisciplina(disciplinas.get(3));
			daoCal.salvar(c);
			
			c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.SEG);
			c.setAula(4);
			c.setDisciplina(disciplinas.get(1));
			daoCal.salvar(c);
			
			c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.SEG);
			c.setAula(5);
			c.setDisciplina(disciplinas.get(1));
			daoCal.salvar(c);
		}
		
		private static void inserirCalendarioTerca (
				List<Disciplina> disciplinas, CalendarioSemanalService daoCal)
				throws CrudException {
			CalendarioSemanal c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.TER);
			c.setAula(1);
			c.setDisciplina(disciplinas.get(2));
			daoCal.salvar(c);
			
			c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.TER);
			c.setAula(2);
			c.setDisciplina(disciplinas.get(1));
			daoCal.salvar(c);
			
			c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.TER);
			c.setAula(3);
			c.setDisciplina(disciplinas.get(3));
			daoCal.salvar(c);
			
			c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.TER);
			c.setAula(4);
			c.setDisciplina(disciplinas.get(0));
			daoCal.salvar(c);
			
			c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.TER);
			c.setAula(5);
			c.setDisciplina(disciplinas.get(4));
			daoCal.salvar(c);
		}
		
		private static void inserirCalendarioQuarta (
				List<Disciplina> disciplinas, CalendarioSemanalService daoCal)
				throws CrudException {
			CalendarioSemanal c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.QUA);
			c.setAula(1);
			c.setDisciplina(disciplinas.get(2));
			daoCal.salvar(c);
			
			c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.QUA);
			c.setAula(2);
			c.setDisciplina(disciplinas.get(2));
			daoCal.salvar(c);
			
			c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.QUA);
			c.setAula(3);
			c.setDisciplina(disciplinas.get(3));
			daoCal.salvar(c);
			
			c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.QUA);
			c.setAula(4);
			c.setDisciplina(disciplinas.get(3));
			daoCal.salvar(c);
			
			c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.QUA);
			c.setAula(5);
			c.setDisciplina(disciplinas.get(3));
			daoCal.salvar(c);
		}
		
		private static void inserirCalendarioQuinta (
				List<Disciplina> disciplinas, CalendarioSemanalService daoCal)
				throws CrudException {
			CalendarioSemanal c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.QUI);
			c.setAula(1);
			c.setDisciplina(disciplinas.get(1));
			daoCal.salvar(c);
			
			c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.QUI);
			c.setAula(2);
			c.setDisciplina(disciplinas.get(1));
			daoCal.salvar(c);
			
			c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.QUI);
			c.setAula(3);
			c.setDisciplina(disciplinas.get(4));
			daoCal.salvar(c);
			
			c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.QUI);
			c.setAula(4);
			c.setDisciplina(disciplinas.get(4));
			daoCal.salvar(c);
			
			c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.QUI);
			c.setAula(5);
			c.setDisciplina(disciplinas.get(0));
			daoCal.salvar(c);
		}
		
		private static void inserirCalendarioSexta (
				List<Disciplina> disciplinas, CalendarioSemanalService daoCal)
				throws CrudException {
			CalendarioSemanal c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.SEX);
			c.setAula(1);
			c.setDisciplina(disciplinas.get(3));
			daoCal.salvar(c);
			
			c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.SEX);
			c.setAula(2);
			c.setDisciplina(disciplinas.get(2));
			daoCal.salvar(c);
			
			c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.SEX);
			c.setAula(3);
			c.setDisciplina(disciplinas.get(2));
			daoCal.salvar(c);
			
			c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.SEX);
			c.setAula(4);
			c.setDisciplina(disciplinas.get(4));
			daoCal.salvar(c);
			
			c = new CalendarioSemanal();
			c.setDia(CalendarioSemanal.SEX);
			c.setAula(5);
			c.setDisciplina(disciplinas.get(4));
			daoCal.salvar(c);
		}
		
		

		static void inserirAluno(Curso curso) throws CrudException {
			AlunoService daoAluno = Factory.getAlunoService();
			Aluno aluno = new Aluno();
			aluno.setNome("Marcos Balsamo");
			aluno.setUsername("marcos");
			aluno.setSenha("marcos");
			aluno.setCurso(curso);
			daoAluno.salvar(aluno);
		}
		
		static void inserirAluno2(Curso curso) throws CrudException {
			AlunoService daoAluno = Factory.getAlunoService();
			Aluno aluno = new Aluno();
			aluno.setNome("Toni Santes");
			aluno.setUsername("toni");
			aluno.setSenha("toni");
			aluno.setCurso(curso);
			daoAluno.salvar(aluno);
		}

		static List<Disciplina> inserirDisciplinas(List<Professor> professores, Curso curso)
				throws CrudException {
			DisciplinaService daoDisc = Factory.getDisciplinaService();
			ArrayList<Disciplina> list = new ArrayList<Disciplina>();
			for (int i=0; i<5; i++) {
				Disciplina disciplina = new Disciplina();
				disciplina.setCurso(curso);
				disciplina.setNome(NOMES_DISCIPLINAS[i]);
				disciplina.setProfessor(professores.get(i));
				daoDisc.salvar(disciplina);
				list.add(disciplina);
			}
			return list;
		}

		static Curso inserirCurso() throws CrudException {
			CursoService daoCurso = Factory.getCursoService();
			
			Curso curso = new Curso();
			curso.setNome("Ciência da computação");
			curso.setVagas(40);
			daoCurso.salvar(curso);
			return curso;
		}

		private static List<Professor> criarProfessores() throws Exception {
			ProfessorService daoProf = Factory.getProfessorService();
			ArrayList<Professor> list = new ArrayList<Professor>();
			for (int i=0; i<5; i++) {
				Professor professor = new Professor();
				professor.setNome(NOMES_PROFESSORES[i]);
				professor.setUsername("prof_"+i);
				professor.setSenha(professor.getUsername());
				
				daoProf.salvar(professor);
				list.add(professor);
			}
			return list;
			
		}
}